<?php namespace Zders\CustomTools\Classes\Traits;

use BackendMenu;
use Backend;

trait SideNav {

    public function register()
    {

        $configuration = $this->getConfigurationFromYaml();
        if (array_key_exists('navigation', $configuration)) {
            $navigation = $configuration['navigation'];
            if (is_array($navigation)) {
                array_walk_recursive($navigation, function (&$item, $key) {
                    if ($key === 'url') {
                        $item = Backend::url($item);
                    }
                });
            }
            foreach ($navigation as $nak => $nav){
                // BackendMenu::registerCallback(function ($manager) use($nak, $nav){
                //     $manager->registerMenuItems('Zder.'.$this->sideNavBaseName, [$nak=>$nav]);
                // });
                BackendMenu::registerContextSidenavPartial(
                    'Zders.'.$this->sideNavBaseName,
                    $nak,
                    '~/plugins/zders/customtools/partials/sidenav/_system_sidebar.htm'
                );
            }
        }
    }

    public function registerSideNavSettings()
    {
        $sideNav=[];
        $configuration = $this->getConfigurationFromYaml();
        if (array_key_exists('navigation', $configuration)) {
            $navigation = $configuration['navigation'];
            if (is_array($navigation)) {
                array_walk_recursive($navigation, function (&$item, $key) {
                    if ($key === 'url') {
                        $item = Backend::url($item);
                    }
                });
            }
            foreach ($navigation as $nak=>$nav){
                $sideMenu=$nav['sideMenu']??[];
                foreach ($sideMenu as $sidek=>&$sidrv){
                    $sidrv['context']=$sidrv['context']??$nak;
                    $sidrv['description']=$sidrv['description']??'';
                }
                $sideNav = array_merge($sideNav, $sideMenu);
            }
        }
        return $sideNav;
    }
}
